package cn.iocoder.yudao.module.of.controller.admin.meetingpart;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.of.controller.admin.meetingpart.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.meetingpart.MeetingPartDO;
import cn.iocoder.yudao.module.of.service.meetingpart.MeetingPartService;

@Tag(name = "管理后台 - 会议参与者")
@RestController
@RequestMapping("/of/meeting-part")
@Validated
public class MeetingPartController {

    @Resource
    private MeetingPartService meetingPartService;

    @PostMapping("/create")
    @Operation(summary = "创建会议参与者")
    @PreAuthorize("@ss.hasPermission('of:meeting-part:create')")
    public CommonResult<Long> createMeetingPart(@Valid @RequestBody MeetingPartSaveReqVO createReqVO) {
        return success(meetingPartService.createMeetingPart(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会议参与者")
    @PreAuthorize("@ss.hasPermission('of:meeting-part:update')")
    public CommonResult<Boolean> updateMeetingPart(@Valid @RequestBody MeetingPartSaveReqVO updateReqVO) {
        meetingPartService.updateMeetingPart(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会议参与者")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('of:meeting-part:delete')")
    public CommonResult<Boolean> deleteMeetingPart(@RequestParam("id") Long id) {
        meetingPartService.deleteMeetingPart(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会议参与者")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:meeting-part:query')")
    public CommonResult<MeetingPartRespVO> getMeetingPart(@RequestParam("id") Long id) {
        MeetingPartDO meetingPart = meetingPartService.getMeetingPart(id);
        return success(BeanUtils.toBean(meetingPart, MeetingPartRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会议参与者分页")
    @PreAuthorize("@ss.hasPermission('of:meeting-part:query')")
    public CommonResult<PageResult<MeetingPartRespVO>> getMeetingPartPage(@Valid MeetingPartPageReqVO pageReqVO) {
        PageResult<MeetingPartDO> pageResult = meetingPartService.getMeetingPartPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MeetingPartRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会议参与者 Excel")
    @PreAuthorize("@ss.hasPermission('of:meeting-part:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMeetingPartExcel(@Valid MeetingPartPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MeetingPartDO> list = meetingPartService.getMeetingPartPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会议参与者.xls", "数据", MeetingPartRespVO.class,
                        BeanUtils.toBean(list, MeetingPartRespVO.class));
    }

}